package com.example.echoewish.ui.home_activity;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.bumptech.glide.Glide;
import com.example.echoewish.pojo.Comment;
import com.example.echoewish.R;
import com.example.echoewish.econstant.Constant;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class CommentAdapter extends ArrayAdapter<Comment> {
    private Context context;
    private int resourceId;

    private TextView timeTextView;
    private TextView nameTextView;
    private TextView contentTextView;


    public CommentAdapter(@NonNull Context context, int resource, @NonNull List<Comment> objects) {
        super(context, resource, objects);
        this.context=context;
        this.resourceId=resource;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view= LayoutInflater.from(context).inflate(resourceId,parent,false);
        CircleImageView headImageView=view.findViewById(R.id.comment_head_image);
        timeTextView=view.findViewById(R.id.comment_time_text_view);
        nameTextView=view.findViewById(R.id.comment_name_text_view);
        contentTextView=view.findViewById(R.id.comment_content_text_view);

        Comment comment=getItem(position);
        if(comment!=null)
        {
            String realTime=comment.time;
            timeTextView.setText(realTime);
            contentTextView.setText(comment.content);
            nameTextView.setText(comment.name);
            if(comment.imageName!=null &&!comment.imageName.equals(""))
            {
                Glide.with(context).load(Constant.HEAD_IMAGE_URL+comment.imageName).into(headImageView);
            }
        }
        return view;
    }

}
